package com.example.mywork1;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
//import android.support.v7.widget.RecycleView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import java.util.Map;


public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private View view;
    private Context context;
    private List<Map<String,Object>> data;
    private Class[] jumpActivity={friendDetail1.class,
            friendDetail2.class};

    //构造
    public MyAdapter(List<Map<String,Object>> data, Context context){
        this.data=data;
        this.context=context;
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view= LayoutInflater.from(context).inflate(R.layout.item,parent,false);
        MyViewHolder myViewHolder=new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        final Map<String,Object> content = data.get(position);
        holder.imageView.setImageResource((int)(data.get(position).get("照片")));
        holder.textView1.setText(data.get(position).get("姓名").toString());
        holder.textView2.setText(data.get(position).get("地区").toString());
        holder.textView3.setText(data.get(position).get("年龄").toString());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "你点击的是：" + content, Toast.LENGTH_SHORT).show();
            }
        });
        //设置item中的点击监听
        int i = holder.getAdapterPosition();
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context,jumpActivity[i]);
                intent.putExtra("name", data.get(i).get("姓名").toString());
                intent.putExtra("area", data.get(i).get("地区").toString());
                intent.putExtra("age", data.get(i).get("年龄").toString());

                context.startActivity(intent);
            }
        });


    }

    @Override
    public int getItemCount() {
        return data.size();
    }



    public class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView1,textView2,textView3;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.imageView);
            textView1=itemView.findViewById(R.id.textView1);
            textView2=itemView.findViewById(R.id.textView2);
            textView3=itemView.findViewById(R.id.textView3);
        }
    }
}